package com.learn.lb.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 单例创建CuratorSession
 *
 * @author laibo
 * @since 2019/9/10 13:33
 */
public class CuratorSessionGenerate {

    private static CuratorFramework instance;

    private CuratorSessionGenerate() {
    }

    public static CuratorFramework getCuratorSession() {
        return Holder.instance;
    }

    private static class Holder {
        /**
         * 操作zk的客户端实例
         */
        private static CuratorFramework instance;

        static {
            instance = CuratorFrameworkFactory.builder()
                    .connectString(ZkProperty.ZK_ADDRESS)
                    .sessionTimeoutMs(ZkProperty.ZK_SESSION_TIMEOUT)
                    .connectionTimeoutMs(ZkProperty.ZK_CONNECT_TIMEOUT)
                    .namespace("app1")
                    .retryPolicy(new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE))
                    .build();
            instance.start();
            System.out.println("zk session success ...");
        }
    }
}
